﻿Imports Heren.Common.Forms.Editor
Imports Heren.Common.Forms
Imports Heren.Common.ScriptEngine.Script
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports Heren.Common.Forms.Runtime
Imports System.Text
Imports Heren.Common.Libraries

Public Class 压疮发生率 : Inherits AbstractRuntimeHandler
    ''****************************************************************
    '' 这是护理电子病历表单后台运行支持脚本
    '' 注意：这是Visual Basic脚本，您可以不区分大小写 
    '' 请不要修改系统提供的方法名，以及方法名前面的Overrides关键字
    '' Script Language : Visual Basic
    '' Author : YangMingkun, Date : 2012-3-18
    '' Copyright (C) Heren Health Services (SUPCON) Co.,Ltd.
    ''****************************************************************

    Private VitalNameTable As Hashtable = Nothing
    Private XueyaTable As Hashtable = Nothing
    Private bFalg As Boolean = False

    '表单脚本初始化
    Public Overrides Sub Initialize()
        Me.VitalNameTable = New Hashtable()
        Me.VitalNameTable.Add("体温_口表", "口温")
        Me.VitalNameTable.Add("体温_肛表", "肛温")
        Me.VitalNameTable.Add("体温_腋表", "腋温")
        Me.VitalNameTable.Add("体温_耳表", "耳温")
        Me.VitalNameTable.Add("体温", "腋温")
        Me.VitalNameTable.Add("口温", "体温_口表")
        Me.VitalNameTable.Add("肛温", "体温_肛表")
        Me.VitalNameTable.Add("腋温", "体温")
        Me.VitalNameTable.Add("耳温", "体温_耳表")
        'Me.ShowMessage("表单已完成加载!")
        '在此函数中来查询和设置表单中的相关数据
        '可以调用GetSystemContext函数获取系统中已有数据，也可以从数据库中读取其他任意数据

    End Sub


    '获取关键数据列表
    Public Overrides Function GetKeyDataList() As List(Of KeyData)
        Dim CheckChuRuLiangNameResult1 As String = Me.CheckChuRuLiangName(CType(Me.FindControl("XDataGrid2"), XDataGrid))
        '    showmessage(CheckChuRuLiangNameResult1)
        If CheckChuRuLiangNameResult1.Contains("小结") OrElse CheckChuRuLiangNameResult1.Contains("总结") Then
            ShowMessage("入量表格中请录入入量明细，请不要包含小结，小结可在护理记录单列表单独插入自动计算！")
            Exit Function
        ElseIf CheckChuRuLiangNameResult1 <> "" Then
            ShowMessage("入量表格中包含相同入量名称，请更改！")
            Exit Function
        End If
        Dim CheckChuRuLiangNameResult2 As String = Me.CheckChuRuLiangName(CType(Me.FindControl("XDataGrid1"), XDataGrid))
        '    showmessage(CheckChuRuLiangNameResult2)
        If CheckChuRuLiangNameResult2.Contains("小结") OrElse CheckChuRuLiangNameResult2.Contains("总结") Then
            ShowMessage("出量表格中请录入出量明细，请不要包含小结，小结可在护理记录单列表单独插入自动计算！")
            Exit Function
        ElseIf CheckChuRuLiangNameResult2 <> "" Then
            ShowMessage("出量表格中包含相同入量名称，请更改！")
            Exit Function
        End If
        Dim keyDataList As New List(Of KeyData)
        keyDataList.Add(New KeyData("意识", Me.GetControlText("XComboBox1"), "字符", ""))
        keyDataList.Add(New KeyData("心律", Me.GetControlText("XTextBox10"), "字符", ""))
        keyDataList.Add(New KeyData("心电监护", Me.GetControlText("XComboBox6"), "字符", ""))
        keyDataList.Add(New KeyData("氧饱和度", Me.GetControlText("XTextBox6"), "数值", "%"))
        keyDataList.Add(New KeyData("血糖", Me.GetControlText("XTextBox1"), "数值", "%"))
        keyDataList.Add(New KeyData("手术敷料", Me.GetControlText("XTextBox2"), "数值", "%"))
        Dim pifuState As New StringBuilder
        If Me.IsCheckButtonChecked("XCheckBox1") Then
            pifuState.Append("完整")
            pifuState.AppendLine()
        End If
        If Me.IsCheckButtonChecked("XCheckBox7") Then
            pifuState.Append("破损")
            pifuState.AppendLine()
        End If
        If Me.IsCheckButtonChecked("XCheckBox4") Then
            pifuState.Append("皮疹")
            pifuState.AppendLine()
        End If
        If Me.IsCheckButtonChecked("XCheckBox3") Then
            pifuState.Append("瘀斑")
            pifuState.AppendLine()
        End If
        If Me.IsCheckButtonChecked("XCheckBox10") Then
            pifuState.Append("红疹")
            pifuState.AppendLine()
        End If
        If Me.IsCheckButtonChecked("XCheckBox6") Then
            pifuState.Append("出血点")
            pifuState.AppendLine()
        End If
        If Me.IsCheckButtonChecked("XCheckBox2") Then
            pifuState.Append("溃疡")
            pifuState.AppendLine()
        End If
        If Me.IsCheckButtonChecked("XCheckBox5") Then
            pifuState.Append("压疮")
            pifuState.AppendLine()
        End If
        If Me.IsCheckButtonChecked("XCheckBox8") Then
            pifuState.Append("水肿")
            pifuState.AppendLine()
        End If
        If Me.IsCheckButtonChecked("XCheckBox9") Then
            pifuState.Append("浸渍")
            pifuState.AppendLine()
        End If
        If Me.IsCheckButtonChecked("XCheckBox11") Then
            pifuState.Append("其他")
            '        pifuState.AppendLine()
        End If
        keyDataList.Add(New KeyData("皮肤", pifuState.toString(), "字符", ""))

        Dim recorddate As DateTime
        Me.GetSystemContext("评估时间", recorddate)
        If Me.GetControlText("XComboBox2").Trim() <> "" Then
            Dim TiWendata1 As New KeyData(Me.GetControlText("XComboBox2"), Me.GetControlText("XTextBox3"), "数值", "℃", 1, recorddate)
            TiWendata1.Category = 1
            keyDataList.Add(TiWendata1)
        End If

        '    keyDataList.Add(New KeyData("体温", Me.GetControlText("XTextBox3"), "数值", "℃"))
        keyDataList.Add(New KeyData(Me.VitalNameTable(Me.GetControlText("XComboBox2")), Me.GetControlText("XTextBox3"), "数值", "℃", 1, recorddate))

        If Me.VitalNameTable(Me.GetControlText("XComboBox2")) <> "体温" Then
            keyDataList.Add(New KeyData("体温", Me.GetControlText("XTextBox3"), "数值", "℃"))
        End If

        keyDataList.Add(New KeyData("脉搏", Me.GetControlText("XTextBox4"), "数值", "次/分", 1, recorddate))
        keyDataList.Add(New KeyData("心率", Me.GetControlText("XTextBox5"), "数值", "次/分", 1, recorddate))
        keyDataList.Add(New KeyData("呼吸", Me.GetControlText("XTextBox7"), "数值", "次/分", 1, recorddate))

        '    Dim dtFlag As DateTime = New DateTime(recorddate.Year, recorddate.Month, recorddate.Day, 12,0,0).ToString("yyyy-MM-dd HH:mm:ss")
        '    If recorddate < dtFlag Then
        '         keyDataList.Add(New KeyData("血压high", Me.GetControlText("XTextBox8"), "数值", "mmHg", 1,recorddate.Date))
        '         keyDataList.Add(New KeyData("血压Low", Me.GetControlText("XTextBox9"), "数值", "mmHg", 1,recorddate.Date))      
        '    Else If recorddate > dtFlag Then
        '         keyDataList.Add(New KeyData("血压high2", Me.GetControlText("XTextBox8"), "数值", "mmHg", 1,recorddate.Date))
        '         keyDataList.Add(New KeyData("血压Low2", Me.GetControlText("XTextBox9"), "数值", "mmHg", 1,recorddate.Date))           
        '    End If
        '    keyDataList.Add(New KeyData("血压high", Me.GetControlText("XTextBox8"), "数值", "mmHg", 1,recorddate))
        '    keyDataList.Add(New KeyData("血压Low", Me.GetControlText("XTextBox9"), "数值", "mmHg", 1,recorddate))
        Dim xueyaFlag As String = Me.GetControlText("XComboBox3").trim()
        If xueyaFlag <> "" AndAlso xueyaFlag = "早血压" Then
            keyDataList.Add(New KeyData("血压high", Me.GetControlText("XTextBox8"), "字符", "mmHg", 1, recorddate.Date))
            keyDataList.Add(New KeyData("血压Low", Me.GetControlText("XTextBox9"), "字符", "mmHg", 1, recorddate.Date))
        ElseIf xueyaFlag <> "" AndAlso xueyaFlag = "晚血压" Then
            keyDataList.Add(New KeyData("血压high2", Me.GetControlText("XTextBox8"), "字符", "mmHg", 1, recorddate.Date))
            keyDataList.Add(New KeyData("血压Low2", Me.GetControlText("XTextBox9"), "字符", "mmHg", 1, recorddate.Date))
        ElseIf xueyaFlag <> "" AndAlso xueyaFlag = "血压3" Then
            keyDataList.Add(New KeyData("血压high3", Me.GetControlText("XTextBox8"), "字符", "mmHg", 1, recorddate.Date))
            keyDataList.Add(New KeyData("血压Low3", Me.GetControlText("XTextBox9"), "字符", "mmHg", 1, recorddate.Date))
        ElseIf xueyaFlag <> "" AndAlso xueyaFlag = "血压4" Then
            Dim time As DateTime = New Date(recorddate.year, recorddate.month, recorddate.day, 0, 0, 0)
            keyDataList.Add(New KeyData("血压high4", Me.GetControlText("XTextBox8"), "字符", "mmHg", 1, time))
            keyDataList.Add(New KeyData("血压Low4", Me.GetControlText("XTextBox9"), "字符", "mmHg", 1, time))
        ElseIf xueyaFlag <> "" AndAlso xueyaFlag = "血压5" Then
            keyDataList.Add(New KeyData("血压high5", Me.GetControlText("XTextBox8"), "字符", "mmHg", 1, recorddate.Date))
            keyDataList.Add(New KeyData("血压Low5", Me.GetControlText("XTextBox9"), "字符", "mmHg", 1, recorddate.Date))
        ElseIf xueyaFlag <> "" AndAlso xueyaFlag = "血压6" Then
            keyDataList.Add(New KeyData("血压high6", Me.GetControlText("XTextBox8"), "字符", "mmHg", 1, recorddate.Date))
            keyDataList.Add(New KeyData("血压Low6", Me.GetControlText("XTextBox9"), "字符", "mmHg", 1, recorddate.Date))
        End If


        Dim column1_value As New StringBuilder
        Dim column2_value As New StringBuilder
        Dim column3_value As New StringBuilder
        Dim column4_value As New StringBuilder
        Dim column5_value As New StringBuilder

        Dim datagrid As XDataGrid = Me.FindControl("XDataGrid2")
        For index As Integer = 0 To datagrid.Rows.Count - 1
            Dim cell1Value As String = datagrid.GetCellValue(index, 0, "")
            Dim cell2Value As String = String.Empty
            Dim cell3Value As String = String.Empty
            Dim cell4Value As String = String.Empty
            Dim cell5Value As String = String.Empty
            Dim cell6Value As String = String.Empty
            Dim cell7Value As String = String.Empty

            If cell1Value.Trim() = "" Then
                Continue For
            End If
            cell2Value = datagrid.GetCellValue(index, 1, 0)
            cell3Value = datagrid.GetCellValue(index, 2, 0)
            cell4Value = datagrid.GetCellValue(index, 3, 0)
            cell5Value = datagrid.GetCellValue(index, 4, "")
            cell6Value = datagrid.GetCellValue(index, 5, "")
            cell7Value = datagrid.GetCellValue(index, 6, "")
            '加入判断


            If cell4Value.toString().Trim() <> "" Then
                column1_value.Append(cell1Value)
                If index < datagrid.Rows.Count - 1 Then column1_value.AppendLine()
                column2_value.Append(cell2Value)
                If index < datagrid.Rows.Count - 1 Then column2_value.AppendLine()
                column3_value.Append(cell3Value)
                If index < datagrid.Rows.Count - 1 Then column3_value.AppendLine()
                column4_value.Append(cell4Value)
                If index < datagrid.Rows.Count - 1 Then column4_value.AppendLine()
                column5_value.Append(cell5Value)
                If index < datagrid.Rows.Count - 1 Then column5_value.AppendLine()
                dataGrid.SetRowState(index, 0)
            End If
            keyDataList.Add(New KeyData(cell1Value, cell4Value.Trim(), "数值", "ml", 1, recorddate, "入量"))
        Next
        keyDataList.Add(New KeyData("入量名称", column1_value.ToString(), "字符", ""))
        'keyDataList.Add(New KeyData("", column2_value.ToString(), "字符", ""))
        keyDataList.Add(New KeyData("入量(备用量)", column3_value.ToString(), "数值", "ml"))
        keyDataList.Add(New KeyData("入量", column4_value.ToString(), "数值", "ml"))
        keyDataList.Add(New KeyData("途径", column5_value.ToString(), "字符", "ml"))



        column1_value.Remove(0, column1_value.Length)
        column2_value.Remove(0, column2_value.Length)
        datagrid = Me.FindControl("XDataGrid1")
        For index As Integer = 0 To datagrid.Rows.Count - 1
            Dim cellValue As String = datagrid.GetCellValue(index, 0, "")
            Dim cell2Value As String = String.Empty
            If cellValue.Trim() = "" Then
                Continue For
            End If
            cell2Value = datagrid.GetCellValue(index, 1, 0)
            If cell2Value.toString().Trim() <> "" Then
                column1_value.Append(cellValue)
                If index < datagrid.Rows.Count - 1 Then column1_value.AppendLine()
                column2_value.Append(cell2Value)
                If index < datagrid.Rows.Count - 1 Then column2_value.AppendLine()
                dataGrid.SetRowState(index, 0)
            End If
            keyDataList.Add(New KeyData(cellValue, cell2Value.Trim(), "数值", "ml", 1, recorddate, "出量"))
        Next
        keyDataList.Add(New KeyData("出量名称", column1_value.ToString(), "字符", ""))
        keyDataList.Add(New KeyData("出量", column2_value.ToString(), "数值", "ml"))

        Return keyDataList
    End Function

    ''同步体征数据
    Public Sub SyncPhysicalData(ByVal recorddate As DateTime)
        Me.bFalg = False
        Dim value As Object
        Me.GetSystemContext("病人ID号", value)
        Dim szPatientID As String = value

        Me.GetSystemContext("入院次", value)
        Dim szVisitID As String = value

        Dim sql As New StringBuilder
        sql.Append("select recording_date, time_point, vital_signs, vital_signs_values ")
        sql.Append("from vital_signs_rec@link_emr ")
        sql.AppendFormat("where patient_id='{0}' and visit_id='{1}'", szPatientID, szVisitID)
        sql.AppendFormat(" and time_point=to_date('{0}', 'yyyy-mm-dd hh24:mi:ss')", New DateTime(recorddate.Year, recorddate.Month, recorddate.Day, recorddate.Hour, recorddate.Minute, 0).ToString("yyyy-MM-dd HH:mm:ss"))

        Dim dataTemp As New DataSet
        Me.ExecuteQuery(sql.ToString(), dataTemp)
        If dataTemp Is Nothing OrElse dataTemp.Tables.Count <= 0 Then
            Exit Sub
        End If

        '    If dataTemp.Tables(0).Rows.Count > 0 Then
        '		    Me.SetControlText("XTextBox3", "")
        '		    Me.SetControlText("XTextBox4",  "")
        '		    Me.SetControlText("XTextBox5",  "")
        '		    Me.SetControlText("XTextBox7",  "") 
        '    End If

        For Each row As DataRow In dataTemp.Tables(0).Rows
            Dim strTemp As String = Nothing
            If row.IsNull(3) OrElse row(3).toString() = "" Then
                Continue For
            End If
            strTemp = Double.Parse(Row(3))
            If row(2) IsNot DBNULL.Value AndAlso (row(2) = "体温" OrElse row(2) = "体温_肛表" _
              OrElse row(2) = "体温_耳表" OrElse row(2) = "体温_口表") Then
                Me.SetControlText("XComboBox2", Me.VitalNameTable(row(2)))
                If strTemp <> Me.GetControlText("XTextBox3").Trim() Then
                    Me.bFalg = True
                    Me.SetControlText("XTextBox3", strTemp)
                End If
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).Contains("脉搏") Then
                If strTemp <> Me.GetControlText("XTextBox4").Trim() Then
                    Me.bFalg = True
                    Me.SetControlText("XTextBox4", strTemp)
                End If
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).Contains("心率") Then
                If strTemp <> Me.GetControlText("XTextBox5").Trim() Then
                    Me.bFalg = True
                    Me.SetControlText("XTextBox5", strTemp)
                End If
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).Contains("呼吸") Then
                If strTemp <> Me.GetControlText("XTextBox7").Trim() Then
                    Me.bFalg = True
                    Me.SetControlText("XTextBox7", strTemp)
                End If
                '        Else If row(2) IsNot DBNULL.Value AndAlso row(2).Contains("血压high")  Then
                '              Me.SetControlText("XTextBox8", row(3)) 
                '        Else If row(2) IsNot DBNULL.Value AndAlso row(2).Contains("血压Low")  Then
                '              Me.SetControlText("XTextBox9", row(3))                 
            End If
        Next

        sql.Remove(0, sql.Length)
        sql.Append("select recording_date, time_point, vital_signs, vital_signs_values ")
        sql.Append("from vital_signs_rec@link_emr ")
        sql.AppendFormat("where patient_id='{0}' and visit_id='{1}'", szPatientID, szVisitID)
        sql.AppendFormat(" and recording_date=to_date('{0}', 'yyyy-mm-dd hh24:mi:ss')", recorddate.Date)
        '    showMessage(sql.toString())
        dataTemp = Nothing
        Me.ExecuteQuery(sql.ToString(), dataTemp)
        If dataTemp Is Nothing OrElse dataTemp.Tables.Count <= 0 Then
            Exit Sub
        End If

        '    If dataTemp.Tables(0).Rows.Count > 0 Then
        '		    Me.SetControlText("XTextBox8",  "") 
        '		    Me.SetControlText("XTextBox9",  "")
        '    End If
        '    
        '    Dim dtFlag As DateTime = New DateTime(recorddate.Year, recorddate.Month, recorddate.Day, 12,0,0).ToString("yyyy-MM-dd HH:mm:ss")
        '    
        '    For Each row As DataRow In dataTemp.Tables(0).Rows
        '        Dim strTemp As String = Nothing
        '        If row.IsNull(3) OrElse row(3).toString() = "" Then
        '            Continue For
        '        End If
        '        strTemp = Double.Parse(Row(3))       
        '          If row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压high" AndAlso  recorddate < dtFlag Then
        '             If strTemp <>  Me.GetControlText("XTextBox8").Trim() Then
        '                  Me.bFalg = true
        '                  Me.SetControlText("XTextBox8", strTemp)
        '             End If     
        '          Else If row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压Low" AndAlso  recorddate < dtFlag Then
        '             If strTemp <>  Me.GetControlText("XTextBox9").Trim()  Then
        '                  Me.bFalg = true
        '                  Me.SetControlText("XTextBox9", strTemp)
        '             End If     
        '          Else If row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压high2" AndAlso  recorddate > dtFlag Then
        '              If strTemp <>  Me.GetControlText("XTextBox8").Trim()  Then
        '                  Me.bFalg = true
        '                  Me.SetControlText("XTextBox8", strTemp)
        '              End If    
        '          Else If row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压Low2" AndAlso  recorddate > dtFlag Then  
        '              If strTemp <>  Me.GetControlText("XTextBox9").Trim() Then
        '                   Me.bFalg = true
        '                   Me.SetControlText("XTextBox9", strTemp)
        '              End If     
        '          End If
        '    Next    
        Me.XueyaTable = New Hashtable()
        Dim dtFlag As DateTime = New DateTime(recorddate.Year, recorddate.Month, recorddate.Day, 12, 0, 0).ToString("yyyy-MM-dd HH:mm:ss")
        '    showMessage(recorddate.toString() + "    " +  dtFlag.toString())
        For Each row As DataRow In dataTemp.Tables(0).Rows
            Dim strTemp As String = Nothing
            If row.IsNull(3) OrElse row(3).toString() = "" Then
                Continue For
            End If
            strTemp = Double.Parse(Row(3))
            If row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压high" Then
                '              If strTemp <>  Me.GetControlText("XTextBox8").Trim() Then     
                '                   bFalg = true
                '                   Me.SetControlText("XComboBox1", "血压1")
                '                   Me.SetControlText("XTextBox8", strTemp)
                If Me.XueyaTable.Contains("血压high") Then
                    Me.XueyaTable("血压high") = strTemp
                Else
                    Me.XueyaTable.Add("血压high", strTemp)
                End If
                '              End If       
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压Low" Then
                '              If strTemp <>  Me.GetControlText("XTextBox4").Trim()  Then
                '                   bFalg = true
                '                   Me.SetControlText("XComboBox1", "血压1")
                '                   Me.SetControlText("XTextBox4", strTemp)  
                If Me.XueyaTable.Contains("血压Low") Then
                    Me.XueyaTable("血压Low") = strTemp
                Else
                    Me.XueyaTable.Add("血压Low", strTemp)
                End If
                '              End If            

            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压high2" Then
                '              If strTemp <>  Me.GetControlText("XTextBox8").Trim()  Then
                '                   bFalg = true
                '                   Me.SetControlText("XComboBox1", "血压2")
                '                   Me.SetControlText("XTextBox8",strTemp) 
                If Me.XueyaTable.Contains("血压high2") Then
                    Me.XueyaTable("血压high2") = strTemp
                Else
                    Me.XueyaTable.Add("血压high2", strTemp)
                End If
                '              End If   
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压Low2" Then
                '              If strTemp <>  Me.GetControlText("XTextBox4").Trim()  Then
                '                   bFalg = true
                '                   Me.SetControlText("XComboBox1", "血压2")
                '                   Me.SetControlText("XTextBox4", strTemp)  
                If Me.XueyaTable.Contains("血压Low2") Then
                    Me.XueyaTable("血压Low2") = strTemp
                Else
                    Me.XueyaTable.Add("血压Low2", strTemp)
                End If
                '              End If 
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压Low3" Then
                '              If strTemp <>  Me.GetControlText("XTextBox4").Trim()  Then
                '                   bFalg = true
                If Me.XueyaTable.Contains("血压Low3") Then
                    Me.XueyaTable("血压Low3") = strTemp
                Else
                    Me.XueyaTable.Add("血压Low3", strTemp)
                End If
                '              End If       
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压high3" Then
                '              If strTemp <>  Me.GetControlText("XTextBox4").Trim()  Then
                '                   bFalg = true
                If Me.XueyaTable.Contains("血压high3") Then
                    Me.XueyaTable("血压high3") = strTemp
                Else
                    Me.XueyaTable.Add("血压high3", strTemp)
                End If
                '              End If   
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压high4" Then
                If Me.XueyaTable.Contains("血压high4") Then
                    Me.XueyaTable("血压high4") = strTemp
                Else
                    Me.XueyaTable.Add("血压high4", strTemp)
                End If
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压Low4" Then
                '              If strTemp <>  Me.GetControlText("XTextBox4").Trim()  Then
                '                   bFalg = true
                If Me.XueyaTable.Contains("血压Low4") Then
                    Me.XueyaTable("血压Low4") = strTemp
                Else
                    Me.XueyaTable.Add("血压Low4", strTemp)
                End If
                '              End If  
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压high5" Then
                If Me.XueyaTable.Contains("血压high5") Then
                    Me.XueyaTable("血压high5") = strTemp
                Else
                    Me.XueyaTable.Add("血压high5", strTemp)
                End If
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压Low5" Then
                If Me.XueyaTable.Contains("血压Low5") Then
                    Me.XueyaTable("血压Low5") = strTemp
                Else
                    Me.XueyaTable.Add("血压Low5", strTemp)
                End If
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压high6" Then
                '              If strTemp <>  Me.GetControlText("XTextBox4").Trim()  Then
                '                   bFalg = true
                If Me.XueyaTable.Contains("血压high6") Then
                    Me.XueyaTable("血压high6") = strTemp
                Else
                    Me.XueyaTable.Add("血压high6", strTemp)
                End If
                '              End If  
            ElseIf row(2) IsNot DBNULL.Value AndAlso row(2).toString() = "血压Low6" Then
                '              If strTemp <>  Me.GetControlText("XTextBox4").Trim()  Then
                '                   bFalg = true
                If Me.XueyaTable.Contains("血压Low6") Then
                    Me.XueyaTable("血压Low6") = strTemp
                Else
                    Me.XueyaTable.Add("血压Low6", strTemp)
                End If
                '              End If  
            End If
        Next

    End Sub


    '更新表单中指定的数据
    Public Overrides Function UpdateFormData(ByVal param As String, ByVal data As Object) As Boolean
        If param = "更新控件状态" Then
            Me.SetControlEnabled(Me.FindControl("RootControl1"), Me.ToBoolValue(data, False))
            Return bFalg
        ElseIf param = "数据同步" Then
            Dim recorddate As DateTime
            Me.GetSystemContext("评估时间", recorddate)
            Me.SyncPhysicalData(recorddate)
            Return Me.bFalg
        End If
        Return True
    End Function

    '当单元格收到输入焦点，从而成为 DataGridView 中的当前单元格时发生。
    Private Sub XDataGrid2_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        'e.ColumnIndex, System.Int32
        'e.RowIndex, System.Int32
        Dim datagrid As XDataGrid = CType(sender, XDataGrid)
        Dim cellValue1 As String = datagrid.GetCellValue(e.RowIndex, 0, "")
        Dim cellValue2 As String = datagrid.GetCellValue(e.RowIndex, 1, "")
        Dim cellValue3 As String = datagrid.GetCellValue(e.RowIndex, 2, "")
        Dim cellValue4 As String = datagrid.GetCellValue(e.RowIndex, 3, "")
        Dim cellValue5 As String = datagrid.GetCellValue(e.RowIndex, 4, "")
        Dim cellValue6 As String = datagrid.GetCellValue(e.RowIndex, 5, "")
        Dim cellValue7 As String = datagrid.GetCellValue(e.RowIndex, 6, "")
        If e.RowIndex = 0 AndAlso cellValue1 <> "" AndAlso datagrid.CurrentRow.State.toString() = "Normal" Then
            datagrid.CurrentRow.Cells(0).ReadOnly = True
        ElseIf e.RowIndex <> 0 AndAlso cellValue1 <> "" AndAlso datagrid.CurrentRow.State.toString() = "Normal" Then
            datagrid.CurrentRow.Cells(0).ReadOnly = False
        ElseIf e.RowIndex <> 0 AndAlso cellValue2 <> "" AndAlso datagrid.CurrentRow.State.toString() = "Normal" Then
            datagrid.CurrentRow.Cells(0).ReadOnly = True
        ElseIf e.RowIndex <> 0 AndAlso cellValue3 <> "" AndAlso datagrid.CurrentRow.State.toString() = "Normal" Then
            datagrid.CurrentRow.Cells(0).ReadOnly = True
        ElseIf e.RowIndex <> 0 AndAlso cellValue4 <> "" AndAlso datagrid.CurrentRow.State.toString() = "Normal" Then
            datagrid.CurrentRow.Cells(0).ReadOnly = True
        ElseIf e.RowIndex <> 0 AndAlso cellValue5 <> "" AndAlso datagrid.CurrentRow.State.toString() = "Normal" Then
            datagrid.CurrentRow.Cells(0).ReadOnly = True
        End If
    End Sub

    '当单元格收到输入焦点，从而成为XButton1的当前单元格时发生。
    Private Sub XDataGrid1_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        'e.ColumnIndex, System.Int32
        'e.RowIndex, System.Int32
        Dim datagrid As XDataGrid = CType(sender, XDataGrid)
        Dim cellValue1 As String = datagrid.GetCellValue(e.RowIndex, 0, "")
        Dim cellValue2 As String = datagrid.GetCellValue(e.RowIndex, 1, "")
        If e.RowIndex = 0 AndAlso cellValue1 <> "" AndAlso datagrid.CurrentRow.State.toString() = "Normal" Then
            datagrid.CurrentRow.Cells(0).ReadOnly = True
        ElseIf e.RowIndex <> 0 AndAlso cellValue1 <> "" AndAlso datagrid.CurrentRow.State.toString() = "Normal" Then
            datagrid.CurrentRow.Cells(0).ReadOnly = True
        End If
    End Sub

    '入量验证
    Private Sub XDataGrid2_NumericValidating(ByVal sender As System.Object, ByVal e As Heren.Common.Controls.TableView.NumericValidatingEventArgs)
        'e.RowIndex, System.Int32
        'e.ColumnIndex, System.Int32
        'e.Value, System.Object
        'e.IsNumeric, System.Boolean
        'e.IsInteger, System.Boolean
        If e.ColumnIndex = 1 Then
            e.IsNumeric = True
        ElseIf e.ColumnIndex = 2 Then
            e.IsNumeric = True
        End If
    End Sub

    '获取表单中指定的数据
    Public Overrides Function GetFormData(ByVal param As Object) As Object
        If param = "表单摘要" Then
            Return Me.GetFormSummary()
        End If
        Return Nothing
    End Function

    '出量验证
    Private Sub XDataGrid1_NumericValidating(ByVal sender As System.Object, ByVal e As Heren.Common.Controls.TableView.NumericValidatingEventArgs)
        'e.RowIndex, System.Int32
        'e.ColumnIndex, System.Int32
        'e.Value, System.Object
        'e.IsNumeric, System.Boolean
        'e.IsInteger, System.Boolean
        If e.ColumnIndex = 1 Then
            e.IsNumeric = True
        End If
    End Sub

    '入量自动新增改变行头图标
    Private Sub XDataGrid2_RowsAdded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs)
        'e.RowIndex, System.Int32
        'e.RowCount, System.Int32
        CType(sender, XDataGrid).SetRowState(e.RowIndex, 1)
    End Sub

    '出量自动新增改变行头图标
    Private Sub XDataGrid1_RowsAdded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs)
        'e.RowIndex, System.Int32
        'e.RowCount, System.Int32
        CType(sender, XDataGrid).SetRowState(e.RowIndex, 1)
    End Sub

    '删除入量
    Private Sub XButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim patient_id As String = ""
        Me.GetSystemContext("病人ID号", patient_id)
        Dim visit_id As String = ""
        Me.GetSystemContext("入院次", visit_id)
        Dim recorddate As DateTime
        Me.GetSystemContext("评估时间", recorddate)
        Dim dataGrid As XDataGrid = CType(Me.FindControl("XDataGrid2"), XDataGrid)
        Dim cellValue1 As String = ""
        If dataGrid.CurrentRow IsNot Nothing Then
            cellValue1 = dataGrid.CurrentRow.Cells(0).Value
            Me.GetGridDelete()
        End If

        '     dataGrid =Me.GetGrid(dataGrid)

        If cellValue1 Is Nothing OrElse cellValue1 = "" Then
            If dataGrid.CurrentCell IsNot Nothing AndAlso dataGrid.CurrentCell.RowIndex <> dataGrid.Rows.Count - 1 Then
                If dataGrid.CurrentRow IsNot Nothing Then
                    dataGrid.Rows.Remove(dataGrid.CurrentRow)
                End If
            End If
            Exit Sub
        End If
        Dim sql As String = Me.GetSaveSQL(patient_id, visit_id, "0", recorddate, recorddate _
                  , cellValue1, "", "数值", "ML", 1, 1, "入量", "", "")
        If Not Me.ExecuteUpdate(True, sql) Then
            Me.ShowError("入量删除失败！")
            Exit Sub
        End If
        If dataGrid.CurrentCell IsNot Nothing AndAlso dataGrid.CurrentCell.RowIndex <> dataGrid.Rows.Count - 1 Then
            If dataGrid.CurrentRow IsNot Nothing Then
                dataGrid.Rows.Remove(dataGrid.CurrentRow)
            End If
        End If
        Exit Sub
    End Sub
    '处理子医嘱途径为空，并且做好标记 导入医嘱数据处理
    Public Function GetGridInsert(ByVal result As DataTable) As DataTable
        result.columns.Add("途径备份")
        Dim patient_id As String = ""
        Me.GetSystemContext("病人ID号", patient_id)
        Dim visit_id As String = ""
        Me.GetSystemContext("入院次", visit_id)
        Dim sql As New StringBuilder()
        Dim dataOrders As DataSet = Nothing

        If result Is Nothing OrElse result.Rows.Count <= 0 Then
            Return result
        Else


            For i As Integer = 0 To result.Rows.Count - 1
                Dim szOrderNo1 As String = Me.GetFieldValue(result, i, "OrderNo")
                Dim szOrderSubNo1 As String = Me.GetFieldValue(result, i, "OrderSubNO")
                sql = New stringbuilder
                sql.Append(" select * from doctor_orders_v t  ")
                sql.AppendFormat("where t.patient_id='{0}' and t.visit_id= '{1}' and t.order_no ='{2}'", patient_id, visit_id, szOrderNo1)
                '	showmessage(sql.tostring())

                If Not Me.ExecuteQuery(sql.ToString(), dataOrders) Then
                    Continue For
                End If
                If dataOrders Is Nothing OrElse dataOrders.Tables(0).Rows.Count <= 0 Then
                    Continue For
                End If
                Dim count As Integer = 0
                For j As Integer = 0 To result.Rows.Count - 1
                    Dim szOrderNo2 As String = Me.GetFieldValue(result, j, "OrderNo")
                    Dim szOrderSubNo2 As String = Me.GetFieldValue(result, j, "OrderSubNo")
                    If szOrderNo1 = szOrderNo2 AndAlso szOrderSubNo1 = szOrderSubNo2 Then  '是同一条
                        count = count + 1
                        Continue For
                    ElseIf szOrderNo1 = szOrderNo2 AndAlso szOrderSubNo1 <> szOrderSubNo2 Then '存在复合医嘱
                        count = count + 1
                    End If
                Next j
                ' showmessage("count:"+count.tostring()+"dataOrders.count: "+dataOrders.Tables(0).Rows.Count.tostring)
                Result.Rows(i)("途径备份") = Me.GetFieldValue(result, i, "途径")
                If count = dataOrders.Tables(0).Rows.Count AndAlso Me.ToIntValue(szOrderSubNo1, 1) > 1 Then '组合医嘱全部插入，子医嘱途径置为空
                    result.Rows(i)("途径") = ""
                ElseIf count < dataOrders.Tables(0).Rows.Count Then               '
                    result.Rows(i)("Column3") = ""
                End If

            Next i
            Return result
        End If

    End Function
    '处理子医嘱途径为空，并且做好标记 删除医嘱数据处理
    Public Sub GetGridDelete()
        Dim sql As StringBuilder = New StringBuilder
        Dim data As DataSet = New DataSet
        Dim szPatientID As String = ""
        Me.GetSystemContext("病人ID号", szPatientID)
        Dim szVisitID As String = ""
        Me.GetSystemContext("就诊号", szVisitID)

        Dim result As XDataGrid = CType(Me.FindControl("XDataGrid2"), XDataGrid)
        ShowMessage(result.CurrentRow.Index)
            If result.CurrentRow.Cells(5).Value Is Nothing Then
                Return
            End If
            For i As Integer = 0 To result.Rows.Count - 2

                If result.Rows(i).Cells(5).Value Is Nothing Then
                    Continue For
                End If
                Dim szOrderNo1 As String = result.Rows(i).Cells(5).Value.ToString
                Dim szOrderSubNo1 As String = result.Rows(i).Cells(6).Value.ToString

                sql.AppendFormat(" select * from doctor_orders_v t  where t.patient_id='{0}' and t.visit_id= '{1}' and t.order_no ='{2}'", szPatientID, szVisitID, szOrderNo1)
                If Not Me.ExecuteQuery(sql.ToString(), data) Then
                    Continue For
                End If
                Dim count As Integer = 0
                For j As Integer = 0 To result.Rows.Count - 2
                    
                    If result.Rows(j).Cells(5).Value Is Nothing Then
                        Continue For
                    End If
                    Dim szOrderNo2 As String = result.Rows(j).Cells(5).Value.ToString
                    Dim szOrderSubNo2 As String = result.Rows(j).Cells(6).Value.ToString
                   
                    If i <> result.CurrentRow.Index Then '只有当索引不等于当前选中的删除行是才有必要统计复合医嘱个数
                        If szOrderNo1 = szOrderNo2 AndAlso szOrderSubNo1 = szOrderSubNo2 Then  '
                            count = count + 1
                        ElseIf szOrderNo1 = szOrderNo2 AndAlso szOrderSubNo1 <> szOrderSubNo2 Then '存在复合医嘱
                            count = count + 1
                        End If
                    End If
                Next j

                If count < data.Tables(0).Rows.Count Then               '复合医嘱不全，去除标记 还原途径

                    result.Rows(i).Cells(1).Value = ""
                    result.Rows(i).Cells(4).Value = result.Rows(i).Cells(7).Value

                End If
            Next i

        result.Refresh()
    End Sub
    '删除出量
    Private Sub XButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim patient_id As String = ""
        Me.GetSystemContext("病人ID号", patient_id)
        Dim visit_id As String = ""
        Me.GetSystemContext("入院次", visit_id)
        Dim recorddate As DateTime
        Me.GetSystemContext("评估时间", recorddate)
        Dim dataGrid As XDataGrid = CType(Me.FindControl("XDataGrid1"), XDataGrid)
        Dim cellValue1 As String = ""
        If dataGrid.CurrentRow IsNot Nothing Then
            cellValue1 = dataGrid.CurrentRow.Cells(0).Value
        End If

        If cellValue1 Is Nothing OrElse cellValue1 = "" Then
            If dataGrid.CurrentCell IsNot Nothing AndAlso dataGrid.CurrentCell.RowIndex <> dataGrid.Rows.Count - 1 Then
                If dataGrid.CurrentRow IsNot Nothing Then
                    dataGrid.Rows.Remove(dataGrid.CurrentRow)
                End If
            End If
            Exit Sub
        End If
        Dim sql As String = Me.GetSaveSQL(patient_id, visit_id, "0", recorddate, recorddate _
                  , cellValue1, "", "数值", "ML", 1, 1, "入量", "", "")
        If Not Me.ExecuteUpdate(True, sql) Then
            Me.ShowError("出量删除失败！")
            Exit Sub
        End If
        If dataGrid.CurrentCell IsNot Nothing AndAlso dataGrid.CurrentCell.RowIndex <> dataGrid.Rows.Count - 1 Then
            If dataGrid.CurrentRow IsNot Nothing Then
                dataGrid.Rows.Remove(dataGrid.CurrentRow)
            End If
        End If
        Exit Sub
    End Sub

    '新增入量
    Private Sub XButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim dataGrid As XDataGrid = CType(Me.FindControl("XDataGrid2"), XDataGrid)
        Dim rowIndex As Integer = dataGrid.Rows.Add()
        dataGrid.SetRowState(rowIndex, 1)
    End Sub

    '新增出量
    Private Sub XButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim dataGrid As XDataGrid = CType(Me.FindControl("XDataGrid1"), XDataGrid)
        Dim rowIndex As Integer = dataGrid.Rows.Add()
        dataGrid.SetRowState(rowIndex, 1)
    End Sub

    '组织指定的体征数据保存时的SQL
    Private Function GetSaveSQL(ByVal patientId As String, _
                                ByVal visitId As String, _
                                ByVal subId As String, _
                                ByVal recordDate As DateTime, _
                                ByVal recordTime As DateTime, _
                                ByVal recordName As String, _
                                ByVal recordData As String, _
                                ByVal dataType As String, _
                                ByVal dataUnit As String, _
                                ByVal category As Integer, _
                                ByVal containsTime As Integer, _
                                ByVal remarks As String, _
                                ByVal sourceTag As String, _
                                ByVal sourceType As String) As String

        Return String.Format("SaveVitalSignsData('{0}', '{1}', '{2}', {3}, {4}, '{5}', '{6}', '{7}', '{8}', {9}, {10}, '{11}','{12}','{13}')" _
                    , patientId, visitId, subId _
                    , String.Format("to_date('{0}', 'yyyy-mm-dd hh24:mi:ss')", recordDate.Date.ToString("yyyy-MM-dd")) _
                    , String.Format("to_date('{0}', 'yyyy-mm-dd hh24:mi:ss')", recordTime.ToString("yyyy-MM-dd HH:mm:ss")) _
                    , recordName, recordData, dataType, dataUnit, category, containsTime, remarks, sourceTag, sourceType)
    End Function

    '单击组件时发生。
    Private Sub XButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim result As DataTable = Nothing
        Me.raisecustomevent(sender, "导入医嘱记录", Nothing, Nothing, result)
        If result Is Nothing OrElse result.Rows.Count <= 0 Then
            Exit Sub
        End If
        result = Me.GetGridInsert(result)
        Dim XGrid As XDataGrid = CType(Me.FindControl("XDataGrid2"), XDataGrid)
        For index As Integer = 0 To result.Rows.Count - 1
            Dim gridIndex As Integer = XGrid.Rows.Add()
            Dim gridRow As DataGridViewRow = XGrid.Rows(gridIndex)
            gridRow.Cells(0).Value = Me.GetFieldValue(result, index, "医嘱内容")
            gridRow.Cells(1).Value = Me.GetFieldValue(result, index, "Column3")
            gridRow.Cells(2).Value = Me.GetFieldValue(result, index, "入量")
            gridRow.Cells(3).Value = Me.GetFieldValue(result, index, "剂量")
            gridRow.Cells(4).Value = Me.GetFieldValue(result, index, "途径")
            gridRow.Cells(5).Value = Me.GetFieldValue(result, index, "OrderNO")
            gridRow.Cells(6).Value = Me.GetFieldValue(result, index, "OrderSubNO")
            gridRow.Cells(7).Value = Me.GetFieldValue(result, index, "途径备份")
        Next

    End Sub



    Public Function CheckChuRuLiangName(ByVal dataGrid As XDataGrid) As String
        Dim result As New StringBuilder
        For index As Integer = 0 To datagrid.Rows.Count - 1
            Dim cell1Value As String = datagrid.GetCellValue(index, 0, "")
            If cell1Value = "" Then
                Continue For
            End If
            If cell1Value.Contains("小结") OrElse cell1Value.Contains("总结") Then
                result.Remove(0, result.length)
                result.Append(cell1Value)
                Return result.toString()
            End If
            For nextIndex As Integer = index + 1 To datagrid.Rows.Count - 1
                Dim cell2Value As String = datagrid.GetCellValue(nextIndex, 0, "")
                If cell2Value.Contains("小结") OrElse cell2Value.Contains("总结") Then
                    result.Remove(0, result.length)
                    result.Append(cell2Value)
                    Return result.toString()
                End If
                If cell1Value.Trim() = cell2Value.Trim() Then
                    result.Remove(0, result.length)
                    result.Append(cell1Value.Trim())
                    result.AppendLine()
                    Return result.toString()
                End If
            Next nextIndex
        Next index
        Return result.toString()
    End Function

    '获取表单摘要数据
    Public Function GetFormSummary() As String
        Dim control As Control = Me.FindControl("XTextBox1")
        If control IsNot Nothing Then
            Return control.Text
        End If
        Return String.Empty
    End Function


    'Occurs when the component is clicked.
    Private Sub XButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim recorddate As DateTime
        Me.GetSystemContext("评估时间", recorddate)
        Me.SyncPhysicalData(recorddate)
    End Sub


    '在 ListControl 上更改 SelectedValue 属性的值时引发的事件。
    Private Sub XComboBox3_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Me.XueyaTable Is Nothing Then
            Exit Sub
        End If
        Me.SetControlText("XTextBox8", "")
        Me.SetControlText("XTextBox9", "")

        Dim xcbo As XComboBox = CType(sender, XComboBox)
        If xcbo Is Nothing Then
            Exit Sub
        End If
        Dim xcboText As String = xcbo.Text
        Dim xueya1 As String = String.Empty
        Dim xueya2 As String = String.Empty

        If xcboText = "早血压" Then
            xueya1 = Me.XueyaTable("血压high")
            xueya2 = Me.XueyaTable("血压Low")
        ElseIf xcboText = "晚血压" Then
            xueya1 = Me.XueyaTable("血压high2")
            xueya2 = Me.XueyaTable("血压Low2")
        ElseIf xcboText = "血压3" Then
            xueya1 = Me.XueyaTable("血压high3")
            xueya2 = Me.XueyaTable("血压Low3")
        ElseIf xcboText = "血压4" Then
            xueya1 = Me.XueyaTable("血压high4")
            xueya2 = Me.XueyaTable("血压Low4")
        ElseIf xcboText = "血压5" Then
            xueya1 = Me.XueyaTable("血压high5")
            xueya2 = Me.XueyaTable("血压Low5")
        ElseIf xcboText = "血压6" Then
            xueya1 = Me.XueyaTable("血压high6")
            xueya2 = Me.XueyaTable("血压Low6")
        End If

        If Not String.IsNullOrEmpty(xueya1) OrElse Not String.IsNullOrEmpty(xueya2) Then
            Me.SetControlText("XTextBox8", xueya1)
            Me.SetControlText("XTextBox9", xueya2)
        End If
    End Sub



End Class
